// app.js
import {
  baseUrl
} from "./api/request";
import updateManager from "./utils/updateManager"
App({
  pagePath: "/pages/index/index", //当前页面路径
  onLaunch() {
    updateManager()
    const that = this;
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏高度 + 44
    // (1) 手机端正常，pc端显示有问题
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    // (2) 手机端，pc端都正常
    that.globalData.navBarHeight = systemInfo.statusBarHeight + menuButtonInfo.height + (menuButtonInfo.top - systemInfo.statusBarHeight) * 2;
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuTop = menuButtonInfo.top;
    that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    that.globalData.menuHeight = menuButtonInfo.height;
    that.globalData.menuWidth = menuButtonInfo.width;
    this.menu = wx.getMenuButtonBoundingClientRect()
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
 /*  handleLaunch() {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        console.log('onCheckForUpdate====', res)
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                console.log('success====', res)
                // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    }
  }, */
  isToken() {
    return !!wx.getStorageSync('token')
  },

  setToken(token, userInfo) {
    this.globalData.token = token
    this.globalData.userInfo = userInfo
    wx.setStorageSync('token', token)
    wx.setStorageSync("userInfo", userInfo)
  },
  setUserInfo(options) {
    this.globalData.userInfo = options
    wx.setStorageSync('userInfo', options)
  },
  setUserInfoPhone(options) {
    this.globalData.userInfo.phone = options
    wx.setStorageSync('userInfo.phone', options)
  },


  globalData: {
    use_name: null,
    password: null,
    token: "",
    baseUrl: baseUrl,
    commonUpdate: '/api/user/user/upload',
    commonUploadDoctor: '/api/doctor/upload',
    commonUpload: '/storage/upload/',

    userInfo: {},
    navBarHeight: 0, // 导航栏高度
    menuRight: 0, // 胶囊距右方间距（方保持左、右间距一致）
    menuTop: 0, // 胶囊距上方间距
    menuBotton: 0, // 胶囊距底部间距（保持底部间距一致）
    menuHeight: 0, // 胶囊高度（自定义内容可与胶囊高度保证一致）
    menuWidth: 0, // 胶囊宽度
  }
})